package com.zmn.plat.admin.controller.channel.product.map;

import java.util.List;
import java.util.Optional;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.product.ServProduct;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.zmn.plat.model.entity.channel.product.map.ChannelProductMap;
import com.zmn.plat.model.entity.channel.product.map.ChannelProductMapQuery;



/**
 * 渠道产品映射表
 *
 * @author linfeng
 *
 * @since 2020/10/27 15:48
 */
@Controller
@RequestMapping("/channel/product/map/")
public class ChannelProductMapListController extends ChannelProductMapBaseController {

    /**
     * 列表
     * @author linfeng
     * @param query
     * @since 2020/10/28 11:08
     */
    @RequestMapping("list")
    @RequiresPermissions("/channel/product/map/list.action")
    public ModelAndView list(@ModelAttribute ChannelProductMapQuery query){

        List<ChannelProductMap> items = channelProductMapService.listPageByQuery(query);

        Channel channel = Optional.ofNullable(channelService.findByKey(query.getChannelId())).orElse(new Channel());
        ServProduct servProduct = Optional.ofNullable(servProductService.findByKey(query.getProductId())).orElse(new ServProduct());

        ModelAndView mav = new ModelAndView("channel/servproduct/map/list");
        mav.addObject("items", items);
        mav.addObject("servProduct", servProduct);
        mav.addObject("obj", channel);
        mav.addObject("statuses", GlobalDict.getStatusList(false));
        mav.addObject("vo", query);
        return mav;
    }

    @RequestMapping(value = "product")
    @RequiresPermissions("/channel/product/map/product.action")
    public ModelAndView detail(@RequestParam(value = "channelId", defaultValue = "0") Integer channelId,
                               @RequestParam(value = "productId", defaultValue = "0") Integer productId,
                               @RequestParam(value = "relateId", defaultValue = "0") Integer relateId) {
        ModelAndView mav = new ModelAndView("channel/servproduct/map/mappingChannelProduct");

        ChannelProductMap channelProductMap = null;
        if (!NumberUtil.isNullOrZero(relateId)) {
            channelProductMap = Optional.ofNullable(channelProductMapService.findByKey(relateId)).orElse(new ChannelProductMap());
        } else {
            channelProductMap = new ChannelProductMap();
        }

        channelProductMap.setChannelId(channelId);
        channelProductMap.setProductId(productId);

        mav.addObject("channelProductMap", channelProductMap);
        mav.addObject("productId", productId);
        mav.addObject("channelId", channelId);
        return mav;
    }

}
